import { axiosResourceIns } from '../axios';

const URL: Record<keyof IService.Resource.HTTP, string> = {
    getDeviceLibraryList: '/device/devlib-file-manager/page',
    createDeviceLibrary: '/device/devlib-file-manager/add',
    deleteDeviceLibrary: '/device/devlib-file-manager/delete',
    getDeviceLibraryFileUploadUrl: '/device/devlib-file-manager/file/getHWYunUploadAuthUrlAndFileId',
    getDeviceLibraryFileDownloadUrl: '/device/devlib-file-manager/file/getFileDownloadAuthUrlHUAWEI',
    updateDeviceLibraryFile: '/device/devlib-file-manager/file/upload',
    getSceneLibraryList: '/scenes/sceneslib-file-manager/page',
    createSceneLibrary: '/scenes/sceneslib-file-manager/add',
    deleteSceneLibrary: '/scenes/sceneslib-file-manager/delete',
    getSceneLibraryFileUploadUrl: '/scenes/sceneslib-file-manager/file/getHWYunUploadAuthUrlAndFileId',
    getSceneLibraryFileDownloadUrl: '/scenes/sceneslib-file-manager/file/getFileDownloadAuthUrlHUAWEI',
    updateSceneLibraryFile: '/scenes/sceneslib-file-manager/file/upload',
    getDeviceLibraryDetailInfo: '/device/devlib-file-manager/detail',
    editeDeviceLibrary: '/device/devlib-file-manager/edit',
    editScenesLibrary: '/scenes/sceneslib-file-manager/edit',
    getScenesLibraryDetailInfo: '/scenes/sceneslib-file-manager/detail',

    getProductReviewList: '/dp/product/page',
    getProductReviewDetail: '/dp/product/detail',
    updateProductReviewExamine: '/dp/product/examine',
    getProductReviewExamineList: '/dp/product/examineList',
    getProductProfileDownload: '/dp/product/profileDownload'
};

export async function getDeviceLibraryList(request: IService.Resource.HTTP['getDeviceLibraryList']['request']): Promise<IService.Resource.HTTP['getDeviceLibraryList']['response']> {
    return axiosResourceIns.post(URL.getDeviceLibraryList, { searchKey: request.searchKey }, { params: { current: request.current, size: request.size } });
}

export async function createDeviceLibrary(request: IService.Resource.HTTP['createDeviceLibrary']['request']): Promise<IService.Resource.HTTP['getDeviceLibraryList']['response']> {
    return axiosResourceIns.post(URL.createDeviceLibrary, request);
}

export async function deleteDeviceLibrary(request: IService.Resource.HTTP['deleteDeviceLibrary']['request']): Promise<IService.Resource.HTTP['deleteDeviceLibrary']['response']> {
    return axiosResourceIns.post(URL.deleteDeviceLibrary, request);
}

export async function getDeviceLibraryFileUploadUrl(
    request: IService.Resource.HTTP['getDeviceLibraryFileUploadUrl']['request']
): Promise<IService.Resource.HTTP['getDeviceLibraryFileUploadUrl']['response']> {
    return axiosResourceIns.post(URL.getDeviceLibraryFileUploadUrl, request);
}

export async function getDeviceLibraryFileDownloadUrl(
    request: IService.Resource.HTTP['getDeviceLibraryFileDownloadUrl']['request']
): Promise<IService.Resource.HTTP['getDeviceLibraryFileDownloadUrl']['response']> {
    return axiosResourceIns.post(URL.getDeviceLibraryFileDownloadUrl, request);
}

export async function updateDeviceLibraryFile(request: IService.Resource.HTTP['updateDeviceLibraryFile']['request']): Promise<IService.Resource.HTTP['updateDeviceLibraryFile']['response']> {
    const formData = new FormData();
    formData.append('fileId', request.fileId);
    formData.append('file', request.file);
    return axiosResourceIns.post(URL.updateDeviceLibraryFile, request, { headers: { 'Content-Type': 'multipart/form-data' } });
}

export async function editeDeviceLibrary(request: IService.Resource.HTTP['editeDeviceLibrary']['request']): Promise<IService.Resource.HTTP['editeDeviceLibrary']['response']> {
    return axiosResourceIns.post(URL.editeDeviceLibrary, request);
}

export async function getDeviceLibraryDetailInfo(
    request: IService.Resource.HTTP['getDeviceLibraryDetailInfo']['request']
): Promise<IService.Resource.HTTP['getDeviceLibraryDetailInfo']['response']> {
    return axiosResourceIns.get(URL.getDeviceLibraryDetailInfo,  { params: request });
}

export async function getSceneLibraryList(request: IService.Resource.HTTP['getSceneLibraryList']['request']): Promise<IService.Resource.HTTP['getSceneLibraryList']['response']> {
    return axiosResourceIns.post(URL.getSceneLibraryList, { searchKey: request.searchKey }, { params: { current: request.current, size: request.size } });
}

export async function createSceneLibrary(request: IService.Resource.HTTP['createSceneLibrary']['request']): Promise<IService.Resource.HTTP['createSceneLibrary']['response']> {
    return axiosResourceIns.post(URL.createSceneLibrary, request);
}

export async function deleteSceneLibrary(request: IService.Resource.HTTP['deleteSceneLibrary']['request']): Promise<IService.Resource.HTTP['deleteSceneLibrary']['response']> {
    return axiosResourceIns.post(URL.deleteSceneLibrary, request);
}

export async function getSceneLibraryFileUploadUrl(
    request: IService.Resource.HTTP['getSceneLibraryFileUploadUrl']['request']
): Promise<IService.Resource.HTTP['getSceneLibraryFileUploadUrl']['response']> {
    return axiosResourceIns.post(URL.getSceneLibraryFileUploadUrl, request);
}

export async function getSceneLibraryFileDownloadUrl(
    request: IService.Resource.HTTP['getSceneLibraryFileDownloadUrl']['request']
): Promise<IService.Resource.HTTP['getSceneLibraryFileDownloadUrl']['response']> {
    return axiosResourceIns.post(URL.getSceneLibraryFileDownloadUrl, request);
}

export async function updateSceneLibraryFile(request: IService.Resource.HTTP['updateSceneLibraryFile']['request']): Promise<IService.Resource.HTTP['updateSceneLibraryFile']['response']> {
    const formData = new FormData();
    formData.append('fileId', request.fileId);
    formData.append('file', request.file);
    return axiosResourceIns.post(URL.updateDeviceLibraryFile, request, { headers: { 'Content-Type': 'multipart/form-data' } });
}

export async function editScenesLibrary(request: IService.Resource.HTTP['editScenesLibrary']['request']): Promise<IService.Resource.HTTP['editScenesLibrary']['response']> {
    return axiosResourceIns.post(URL.editScenesLibrary, request);
}

export async function getScenesLibraryDetailInfo(
    request: IService.Resource.HTTP['getScenesLibraryDetailInfo']['request']
): Promise<IService.Resource.HTTP['getScenesLibraryDetailInfo']['response']> {
    return axiosResourceIns.get(URL.getScenesLibraryDetailInfo,  { params: request });
}
    
export async function getProductReviewList(request: IService.Resource.HTTP['getProductReviewList']['request']): Promise<IService.Resource.HTTP['getProductReviewList']['response']> {
    return axiosResourceIns.get(URL.getProductReviewList, { params: request });
}

export async function getProductReviewDetail(request: IService.Resource.HTTP['getProductReviewDetail']['request']): Promise<IService.Resource.HTTP['getProductReviewDetail']['response']> {
    return axiosResourceIns.get(URL.getProductReviewDetail, { params: request });
}

export async function updateProductReviewExamine(request: IService.Resource.HTTP['updateProductReviewExamine']['request']): Promise<IService.Resource.HTTP['updateProductReviewExamine']['response']> {
    return axiosResourceIns.post(URL.updateProductReviewExamine, request);
}

export async function getProductReviewExamineList(request: IService.Resource.HTTP['getProductReviewExamineList']['request']): Promise<IService.Resource.HTTP['getProductReviewExamineList']['response']> {
    return axiosResourceIns.get(URL.getProductReviewExamineList, { params: request });
}

export async function getProductProfileDownload(request: any) {
    return axiosResourceIns.get(URL.getProductProfileDownload, { 
        params: request, responseType: 'blob',
        transformResponse: [(data, headers) => {
            const readableHeaders:any = {};
            for (const [key, value] of Object.entries(headers)) {
            readableHeaders[key.toLowerCase()] = value;
            }
            return { data, headers: readableHeaders };
        }] 
    });
  }
